import pygame
from constant import *
from menuOptions import *

IMAGES_DIR = 'images/menu/'

class Menu(object):
    def __init__(self, main, images_dir, background_file, items_menu, start_x, start_y, diff) :
        self.images_dir = IMAGES_DIR + images_dir
        self.background = self.__loadBackground__(background_file)
        self.items_menu = items_menu
        self.__images = self.__loadImages__()
        self.images_menu = self.__createMenu__()
        self.current_option = None
        self.__start_x, self.__start_y, self.__diff = start_x, start_y, diff
        self.main = None
        self.setMain(main)
        
    def setMain(self, main) :
        self.main = main
            
    def __loadBackground__(self,background_file) :
        if background_file <> None :
            return pygame.image.load(self.images_dir + background_file).convert_alpha()
    
    def __loadImages__(self) :
        images = {}
        for item in self.items_menu :
            images[item] = {}
            images[item]['on'] = pygame.image.load(self.images_dir + item + '_on.png').convert_alpha()    
            images[item]['off'] = pygame.image.load(self.images_dir + item + '_off.png').convert_alpha()    
        return images
        
    def __createMenu__(self, itemSelected=None) :
        menu = {}
        for item in self.items_menu :
            if itemSelected is item :
                menu[item] = self.__images[item]['on']
            else:
                menu[item] = self.__images[item]['off']
        return menu

    def process_MouseClick(self, x_mouse, y_mouse) :
        option = self.optionSelected(x_mouse, y_mouse)
        if self.current_option == None :
            self.setOption(option)
        else :
            if option == self.current_option :
                self.setAction(self.current_option)
            else:
                self.setOption(option)

    def process_MouseMotion(self, x_mouse, y_mouse) :
        option = self.optionSelected(x_mouse, y_mouse)
        self.setOption(option)
        
    def process_KeyReturn(self) :
        if self.current_option <> None :
            self.setAction(self.current_option)
    
    def setOption(self, option):
        self.images_menu = self.__createMenu__(option)
        self.current_option = option
        
    def optionSelected(self, x_mouse, y_mouse) :
        for item in self.items_menu :
            width = self.images_menu[item].get_width()
            height = self.images_menu[item].get_height()
            x_pos, y_pos = self.__getScreenPosition__(item)
            if (x_pos <= x_mouse <= x_pos + width) :
                if (y_pos <= y_mouse <= y_pos + height) :
                    return item
        return None
        
    def paint(self, screen):
        screen.blit(self.background, (0,0))
        for item in self.items_menu :
            try:
                screen_position = self.__getScreenPosition__(item)
                screen.blit(self.images_menu[item], screen_position)
            except:
                pass
                
    def __getScreenPosition__(self, item) :
        try:
            index = self.items_menu.index(item)
            width = self.images_menu[item].get_width()
            height = self.images_menu[item].get_height()
            x = self.getStartX() 
            y = self.getStartY() + index * (height + self.getDiff())           
            return (x, y)
        except:
            pass
            
    def setAction(self,action) :
        self.main.setAction(action)
        
    def getStartX(self) :
        return self.__start_x
        
    def getStartY(self) :
        return self.__start_y

    def getDiff(self) :
        return self.__diff     
